package com.jnshu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.dao.CourseMapper;
import com.jnshu.dao.HistoryMapper;
import com.jnshu.dao.UserCollectionMapper;
import com.jnshu.dao.UserMapper;
import com.jnshu.dto.person.CollectionDto;
import com.jnshu.dto.person.HistoryDto;
import com.jnshu.dto.person.UserBindingDto;
import com.jnshu.dto.user.MyInfoDto;
import com.jnshu.dto.user.MyPersonDto;
import com.jnshu.exception.MyException;
import com.jnshu.pojo.Collect;
import com.jnshu.pojo.History;
import com.jnshu.pojo.User;
import com.jnshu.service.PersonService;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xmp
 * @date 2019/8/18 4:27
 */
@Service
@Slf4j
public class PersonServiceImpl implements PersonService {
    @Autowired(required = false)
    UserMapper userMapper;

    @Autowired(required = false)
    UserCollectionMapper userCollectionMapper;

    @Autowired(required = false)
    CourseMapper courseMapper;

    @Autowired(required = false)
    HistoryMapper historyMapper;

    @Override
    public MyPersonDto selectMyPersonById(long id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null){
            throw new MyException(500,"请求用户或资源不存在");
        }
        MyPersonDto myPersonDto = new MyPersonDto();
        BeanUtils.copyProperties(user,myPersonDto);
        return myPersonDto;
    }

    @Override
    public MyInfoDto selectMyInfoById(long id) {
        User user = userMapper.selectByPrimaryKey(id);
        log.info("user:"+user);
        if (user == null){
            throw new MyException(500,"请求用户或资源不存在");
        }
        MyInfoDto myInfoDto = new MyInfoDto();
        BeanUtils.copyProperties(user,myInfoDto);
        return myInfoDto;
    }

    @Override
    public ResultBean updateMyInfo(User user) {
        if (user == null){
            throw new MyException(500,"请求用户或资源不存在");
        }
        if (userMapper.selectByNickName(user.getNickname(),user.getId())!=null) {
            throw new MyException(500, "用户名重复");
        }
        boolean user1 = userMapper.updateByPrimaryKeySelective(user);
        if (user1){
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.FAILURE);
    }

    @Override
    public PageBean getCollectCourse(int classify,int pageNum, int pageSize, long userId) {
        if (userMapper.selectByPrimaryKey(userId)==null){
            throw new MyException(501,"该用户不存在");
        }
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<CollectionDto> collectionDto = userCollectionMapper.getCollection(userId,classify);
        log.info("CollectionDto:"+collectionDto);
        if (collectionDto != null){
            return RUtil.okDefault(page.getTotal(),collectionDto,pageNum,pageSize);
        }
        return RUtil.errorPage(ResultEnum.FAILURE);
    }

    @Override
    public PageBean getHistory(int type, int pageSize, int pageNum, long userId) {
        if (userMapper.selectByPrimaryKey(userId)==null){
            throw new MyException(501,"该用户不存在");
        }
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<HistoryDto> historyDto = historyMapper.getHistory(userId,type);
        log.info("HistoryDto"+historyDto);
        if (historyDto != null){
            return RUtil.okDefault(page.getTotal(),historyDto,pageNum,pageSize);
        }
        return RUtil.errorPage(ResultEnum.FAILURE);
    }

    @Override
    public ResultBean deleteCollection(long courseId,long userId) {
        log.info("传来的资料id：" + courseId+",传来的用户id："+userId);
        Collect collect = userCollectionMapper.selectStatus(courseId,userId);
        if (collect == null) {
            return RUtil.error(600, "用户未未收藏");
        }else {
            boolean a = userCollectionMapper.deleteStatus(userId, courseId);
            if (a){
                return RUtil.ok();
            }else {
                return RUtil.error();
            }
        }
    }

    @Override
    public ResultBean deleteMoreCollection(long userId,String courseId,String status) {
        log.info("controller传入的用户id："+userId+",传入的资料id："+courseId+",传入的状态"+status);
        if (status.equals("all")){
            boolean a =userCollectionMapper.deleteByUserId(userId);
            if (a){
                return RUtil.ok();
            }else {
                return RUtil.error();
            }
        }
        if (status.equals("more")) {
            String[] ids = courseId.split(",");
            boolean a = userCollectionMapper.deleteMoreId(ids,userId);
            if (a){
                return RUtil.ok();
            }else {
                return RUtil.error();
            }
        }
        return RUtil.error();
    }

    @Override
    public ResultBean deleteHistory(long userId) {
        log.info("111111");
        log.info("传来的用户id："+userId);
        List<History> history = historyMapper.selectHistory(userId);
        log.info("用户的浏览记录history："+history);
        if (history == null) {
            return RUtil.error(600, "用户未收藏");
        }else {
            boolean a = historyMapper.deleteHistory(userId);
            if (a){
                return RUtil.ok();
            }else {
                return RUtil.error();
            }
        }
    }

    @Override
    public ResultBean selectUserBindingInfo(long id) {
        log.info("PersonServiceImpl收到的用户id为；"+id);
        UserBindingDto userBindingDto = userMapper.selectUserBindingInfo(id);
        return RUtil.ok(userBindingDto);
    }
}
